How to Use INDEX MATCH with Excel VBA |
您所在的位置:网站首页 › match vba › How to Use INDEX MATCH with Excel VBA |
You can utilize the built-in Excel Worksheet functions such as the VLOOKUP Function, the CHOOSE Function, and the PMT Function in your VBA code and applications as well. In fact, most of the Excel worksheet functions can be accessed and used in VBA code. Like, use INDEX & MATCH Worksheet Functions in Excel VBA code. Why would you want to use Excel Worksheet functions in your VBA code? Well to extend the functionality of the code you are using. Also, you don’t have to come up with your own functions, unless you really need to, if the functionality is already there. All you basically need to do is access the function you need since it’s already there and there is then no need to reinvent the wheel. So, let’s get started with an example showing how to use INDEX MATCH with Excel VBA. We are going to utilize the INDEX and MATCH Functions in Excel VBA code, in order to create a simple UserForm. Using the form, the user selects the name of the student, and then the corresponding gender of said student and eye color is retrieved and returned. Table of Contents hide Download Practice Workbook 9 Easy Steps to Use INDEX and MATCH Within VBA Code Step1: Apply INDEX and MATCH Functions in Dataset Step 2: Change the Name of B Column into StudentNames Step 3: Open Visual Basic Window Step 4: Change Properties and Add Text Boxes Step 5: Add a Button from Toolbox Step 6: Insert VBA Code Step 7: Insert a Command Button Step 8: View Lookup Code Step 9: Exhibit Final Result Conclusion Review Section: Test Your Understanding Related Articles Download Practice WorkbookPlease download the practice workbook to practice yourself. INDEX-MATCH.xlsx 9 Easy Steps to Use INDEX and MATCH Within VBA CodeThe INDEX and MATCH Functions are often used in combination in formulas, in order to perform advanced lookups. The two in combination offer certain advantages over VLOOKUP. We have already covered in detail, how to use INDEX and MATCH to perform advanced lookups in an Excel workbook as a straight worksheet formula, in a previous tutorial. We are now going to see how to use the INDEX and MATCH Functions together in VBA code, in order to confer similar functionality to the look up UserForm we are going to create. AD Step1: Apply INDEX and MATCH Functions in Dataset We are starting off with two sheets in our macro-enabled workbook. One is an empty sheet called UserForm, the other is a sheet called Student Information, which contains a range showing student names, their corresponding gender, and eye color as shown below.Let’s remind ourselves quickly if we wanted to use the INDEX and MATCH Functions in one formula, in the actual worksheet to give us the gender of the name of the student we want to look up. We would use the following formula: =INDEX(B2:B31, MATCH("Diana Graham", A2:A31, 0))Read More: How to Use INDEX MATCH Formula in Excel (9 Examples) AD Step 2: Change the Name of B Column into StudentNames We will now name the range A2: A31, StudentNames as shown below.Read More: INDEX-MATCH with Multiple Matches in Excel (6 Examples) Step 3: Open Visual Basic Window Now with the UserForm sheet activated, we go to Developer > Code > Visual Basic in order to open the Visual Basic Editor (VBE). Once in the VBE interface, we go to Insert, UserForm as shown below.Read More: How to Create Excel VBA UserForm (with Detailed Steps) AD Step 4: Change Properties and Add Text Boxes Using the Properties Window, we will rename our form to StudentLookup, change the Caption to Lookup Student Information, change the BackColor to light blue, and set the height to 300 px and the width to 350 px. If the Properties Window is not showing up, press the F4 key on your keyboard in order to see it.Read More: How to Use INDEX MATCH with Multiple Criteria in Excel (3 Ways) Similar Readings Excel INDEX MATCH If Cell Contains Text How to Use INDEX MATCH Instead of VLOOKUP in Excel Examples with INDEX-MATCH Formula in Excel (8 Approaches) 22 Macro Examples in Excel VBA Step 5: Add a Button from Toolbox Next, Add a button to the form using the Toolbox. Change the Name of the button to cmdLookUp, the BackColor to light orange, keep the Tahoma font, and change the style to bold, finally change the Caption of the button to Look up Student Details as shown below.Read More: INDEX-MATCH Formula to Find Minimum Value in Excel (4 Ways) AD Step 6: Insert VBA Code Right-click, the newly added button, and select View Code.We start off by declaring three variables and assigning the variant data type to these declared variant data types. The variant data type is a good data type to start with. Because when working with worksheet functions, you may not always be sure of the outputs. Therefore use the variant data type, when you are starting out. ADLater on, it is advisable to use one of the other more specific data types such as integer or string. For more advanced longer code, the variant data type does not use memory as efficiently as the other data types. Variable a draw the value from the option the user selects in the drop-down combo box on the UserForm. If there is no selection, then all the other textboxes are empty. If you select a student name from the combo box on the UserForm, then variable b draws value by using the INDEX Worksheet Function in combination with the MATCH Function in the VBA code, as shown. ADIt looks up the value using basically the same syntax as the worksheet function. When using worksheet functions in VBA, the VBA IntelliSense in this particular case is not very intuitive, therefore familiarity with the syntax gleaned from worksheet knowledge is recommended. Variable c draws value by using the INDEX Worksheet Function in combination with the MATCH Function in the VBA code when the user selects an option from the combo box. Variable b attains value from the gender column in the worksheet, whereas variable c gets value from the Eye colour column in the worksheet. The gender textbox is populated with b’s value and the eye color textbox is populated with c’s value. Read More: Excel VBA Events (A Complete Guideline) AD Step 7: Insert a Command Button Now go to the worksheet called UserForm in your workbook. Format it, as shown below, and insert the image provided by ExcelDemy.Read More: How to Create a UserForm: an Overview AD Step 8: View Lookup Code Right-click the button and select View Code as shown below.Read More: How to Use Excel VBA Userform (2 Suitable Examples) Similar Readings INDEX MATCH vs VLOOKUP Function (9 Practical Examples) Difference Between Subroutine & Function in Excel VBA How to Use VBA User Defined Function (4 Suitable Examples) List of 10 Mostly Used Excel VBA Objects (Attributes & Examples) AD Step 9: Exhibit Final Result Return to the worksheet. Make sure the unchecked Design Mode.![]() Remember to save your workbook as a macro-enabled workbook, if you haven’t done so already and there you have it, we use INDEX & MATCH Worksheet Functions in Excel VBA code in order to create a lookup form. ADRead More: Excel INDEX-MATCH Formula to Return Multiple Values Horizontally ConclusionExcel has many useful worksheet functions, which can be utilized in VBA, as like as, using INDEX & MATCH Worksheet Functions in Excel VBA code. These functions will allow you to extend your VBA code. If you already know how they work in a standard Excel worksheet then the learning curve is not that great by adapting the knowledge for VBA. Accessing the worksheet functions, in one’s VBA code can be a real time-save. Because one does not have to develop custom functions for functionality that is already there. ADPlease feel free to comment and tell us if you use worksheet functions in your VBA code and applications. Review Section: Test Your Understanding1) Setup a simple list in column A of three items namely tangerines, carrots, and oranges, then in the cell next to each item in column B list whether the items in column A are fruits or vegetables, once you’ve completed setting up your sample data, use the INDEX & MATCH combination function to deliver whether carrots are fruits or vegetables. 2) Use this data set from ESPN on the NFL head coaches and the respective team they are coaching. Create a user form that allows a user to input the name of a certain coach in a textbox. Then have the team he is coaching delivered in another textbox when the user clicks submit. Use INDEX & MATCH worksheet function combination within your VBA code. AD Related Articles INDEX-MATCH with Duplicate Values in Excel (3 Quick Methods) Introduction to VBA Features and Applications Using Macro Recorder in Excel (With Easy Steps) How to Use VBA Modules in Excel (8 Simple Ways) What Are Excel Function Arguments (A Detailed Discussion) How to Use IF with INDEX & MATCH Functions in Excel (3 Ways) INDEX MATCH for Multiple Criteria in Rows and Columns in Excel SaveSavedRemoved 0![]() ![]() Tags: Index Match ExcelVBA in Excel We will be happy to hear your thoughts Leave a reply Cancel reply |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |